﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

using LogWriter = MyLibrary.Logger.LogWriterCollection<MyLibrary.Logger.ILogWriter>;


namespace MyLibrary.Logger
{
    /// <summary>
    /// 读取配置的
    /// </summary>
    public class LogConfigurator
    {
        static LogWriter logWriters = null;
        static string xmlPath = String.Empty;

        /// <summary>
        /// 配置文件的路径
        /// </summary>
        public static string ConfigFilePath
        {
            get { return xmlPath; }
            set { xmlPath = value; }
        }
        ///// <summary>
        ///// 
        ///// </summary>
        //public static LogWriter LogWriterManager
        //{
        //    get
        //    {
        //        if (logWriters == null)
        //        {
        //            if (xmlPath == String.Empty)
        //                xmlPath = AppDomain.CurrentDomain.BaseDirectory + @"\Config.xml";
        //            InitLogWriter(xmlPath);
        //            return logWriters;
        //        }
        //        return logWriters;
        //    }
        //}

        #region 构造函数
        static LogConfigurator()
        {

        }
        #endregion

        /// <summary>
        /// 从配置文件中初始化配置文件
        /// </summary>
        /// <param name="xmlPath"></param>
        public static void InitLogWriter(string xmlPath)
        {
            #region 文件的验证
            if (String.IsNullOrEmpty(xmlPath)) throw new ArgumentException("配置文件路径为空");
            if (!File.Exists(xmlPath)) throw new ArgumentException("配置文件在指定的路径上不存在");
            try
            {
                FileInfo fi = new FileInfo(xmlPath);
                if (fi.Extension.ToLower() != "xml")
                    throw new FormatException("配置文件必须是XML格式文件");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            #endregion

            try
            {
                //载入配置文件的XML对象
                XmlDocument doc = new XmlDocument();
                doc.Load(xmlPath);
                
                //获取配置文件中所有的Logger配置节
                XmlNodeList xmlLoggerList = doc.SelectNodes("//Logger");
                if (xmlLoggerList.Count == 0) throw new ArgumentNullException("指定的配置文件中不包含有效的日志写入器配置节");


            }
            catch (XmlException xex)
            {
                throw new XmlException(xex.Message);
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }

        /// <summary>
        /// 卸载所有的日志记录器
        /// </summary>
        public static void UnloadLogWriter()
        {
            if (logWriters == null) return;
            foreach (ILogWriter writer in logWriters)
            {
                writer.CloseWriter();
                writer.Dispose();
            }
            logWriters.Clear();
            logWriters == null;
        }

        /// <summary>
        /// 从配置节中获取
        /// </summary>
        /// <param name="configNode"></param>
        /// <returns></returns>
        private static ILogWriter LogWriterParser(XmlNode configNode)
        {
            //获取ConfigNode的属性,确定日志写入器为何种类型
            if (!ValidateConfigSection(configNode)) return nulll;

            XmlAttribute attr = null;
            attr = configNode["type"];

            if (attr.Value.ToLower() == "ado")
            {
                ADOLogWriter adoLogWriter = new ADOLogWriter();
                adoLogWriter.LogWriterName = attr.Value["name"];

            }
            else if(attr.Value.ToLower() == "file" || attr.Value.ToLower() == "xml")
            {

            }
            else
                return null;
        }

        /// <summary>
        /// TODO:验证配置节点的有效性
        /// </summary>
        /// <param name="configNode">配置节点</param>
        /// <returns>是否为有效节点</returns>
        private static bool ValidateConfigSection(XmlNode configNode)
        {
            return true;

            //XmlAttribute attr = null;
            //attr = configNode.Attributes["name"]; 
            //if (attr == null) return false;
            //if (String.IsNullOrEmpty(attr.Value)) return false;
        }
    }
}
